package com.javazyw.utils.io;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ImageUtil {
	
	
	
	public static void ratoate(String oldPath,String newPath,double degree){
		OutputStream os = null;
		try {
			//ImageUtil.merge( new File("e:/2.jpg"),new File("e:/1.jpg"));
			BufferedImage oldImage = ImageIO.read(new FileInputStream(oldPath));
			int width=oldImage.getHeight(),height=oldImage.getWidth();
			if(degree%180==0){
				width=oldImage.getWidth();
				height=oldImage.getHeight();
			}
			BufferedImage newImage = new BufferedImage(width,height, oldImage.getType());
			Graphics2D graphics = (Graphics2D) newImage.getGraphics();
			graphics.rotate(Math.toRadians(degree), newImage.getWidth() / 2, newImage.getHeight() / 2);
			graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);
			graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
			//ImageIO.write(newImage, "JPG", new FileOutputStream(newPath));
			
			os = new FileOutputStream(newPath);
			ImageIO.write(newImage, "JPG", os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void ratoate(String newPath,double degree, InputStream in){
		OutputStream os = null;
		try {
			//ImageUtil.merge( new File("e:/2.jpg"),new File("e:/1.jpg"));
			BufferedImage oldImage = ImageIO.read(in);
			int width=oldImage.getHeight(),height=oldImage.getWidth();
			if(degree%180==0){
				width=oldImage.getWidth();
				height=oldImage.getHeight();
			}
			BufferedImage newImage = new BufferedImage(width,height, oldImage.getType());
			Graphics2D graphics = (Graphics2D) newImage.getGraphics();
			graphics.rotate(Math.toRadians(degree), newImage.getWidth() / 2, newImage.getHeight() / 2);
			graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);
			graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
			os = new FileOutputStream(newPath);

			ImageIO.write(newImage, "JPG", os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://192.168.1.200/ftp/orderpayment_image/201501210/29F78265F78F43708CB19FC9D039F1BE.png");
		
		InputStream in = url.openStream();
		
		ImageUtil.ratoate("D:/right1.jpg", 90, in);
		in.close();
		System.out.println("--------");
		Thread.sleep(10000000);
	}
	
	
	public static File merge (File invoice, File sign) throws IOException{
		if(invoice == null || sign == null){
			return null;
		}
		return merge(invoice,sign,80,360,invoice.getName());
	}
	
	public static File merge (File invoice, File sign,int x,int y,String fileName) throws IOException{
		BufferedImage bi1 = null;
		BufferedImage bi2 = null;
		bi1 = javax.imageio.ImageIO.read(invoice);
		bi2 = javax.imageio.ImageIO.read(sign);
		
		int width = bi2.getWidth(); 
		int height = bi2.getHeight();
		
		double scale = width/height;
		Double imageWidth = (int) 70 * scale;
		
		Graphics g = bi1.getGraphics();
		g.drawImage(bi2.getScaledInstance(imageWidth.intValue(), 70,  Image.SCALE_SMOOTH), x, y, null);

		File posOrder = new File("temp"+File.separator+fileName);
		//生产用，输出到文件流
		FileOutputStream out = new FileOutputStream(posOrder); 
		
		//测试用，输出到文件系统
//		FileOutputStream out = new FileOutputStream("f:/"+fileName);
		
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(bi1);
		out.close();
		
		return posOrder;
	}
}
